package com.tmt.im.manager.controller.api;

import com.tmt.springboot.common.restful.ResultWrapper;
import com.tmt.im.manager.controller.vo.FriendGroupInfoVo;
import com.tmt.im.manager.controller.vo.NewFriendGroupRequest;
import com.tmt.im.manager.controller.vo.UpdateFriendGroupRequest;
import com.tmt.im.manager.interfaces.IYunXinInterface;
import com.tmt.im.manager.service.IMFriendGroupIgniteService;
import com.tmt.im.manager.service.IMFriendGroupMappingService;
import com.tmt.springboot.common.restful.IExecuteResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 好友分组控制器
 *
 * @Description TODO
 * @Author wuyi
 * @Date 2024/8/3 21:20
 * @Version 1.0
 **/
@Async("asyncTaskExecutor")
@Tag(name = "好友分组相关接口", description = "好友分组创建、删除、向分组中添加用户、从分组中移除用户等相关接口")
@RestController
@RequestMapping("${api-url-prefix}/friendGroup")
public class FriendGroupController {

    private final IMFriendGroupIgniteService imFriendGroupService;
    private final IMFriendGroupMappingService imFriendGroupMappingService;
    private final IYunXinInterface iYunXinInterface;

    public FriendGroupController(IMFriendGroupIgniteService imFriendGroupService,
                                 IMFriendGroupMappingService imFriendGroupMappingService,
                                 IYunXinInterface iYunXinInterface) {
        this.imFriendGroupService = imFriendGroupService;
        this.imFriendGroupMappingService = imFriendGroupMappingService;
        this.iYunXinInterface = iYunXinInterface;
    }

    @Operation(summary = "创建好友分组接口",
            description = "新增好友分组接口",
            operationId = "addFriendGroup",
            parameters = {
                    @Parameter(name = "userId", description = "希望加为好友的用户ID", required = true)
            })
    @PostMapping("/create")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> addFriendGroup(@RequestBody NewFriendGroupRequest request, Principal principal) {
        IExecuteResult<Boolean> result = imFriendGroupService.addFriendGroup(request, principal);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "更新好友分组接口",
            description = "更新好友分组接口",
            operationId = "updateFriendGroup",
            parameters = {
                    @Parameter(name = "request", description = "更新好友分组请求对象", required = true)
            })
    @PostMapping("/update")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> updateFriendGroup(@RequestBody UpdateFriendGroupRequest request, Principal principal) {
        IExecuteResult<Boolean> result = imFriendGroupService.updateFriendGroup(request, principal);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "查询好友分组列表接口",
            description = "查询好友分组列表接口",
            operationId = "listFriendGroup")
    @GetMapping("/list")
    public CompletableFuture<IExecuteResult<List<FriendGroupInfoVo>>> listFriendGroup(Principal principal) {
        IExecuteResult<List<FriendGroupInfoVo>> result = imFriendGroupService.listFriendGroup(principal);
        return CompletableFuture.completedFuture(result);
    }

    @Operation(summary = "删除好友分组接口",
            description = "删除好友分组接口",
            operationId = "deleteFriendGroup",
            parameters = {
                    @Parameter(name = "groupId", description = "要删除的分组ID", required = true)
            })
    @PostMapping("/delete/{groupId}")
    public CompletableFuture<IExecuteResult<ResultWrapper<Boolean>>> deleteFriendGroup(@PathVariable("groupId") Long groupId, Principal principal) {
        IExecuteResult<Boolean> result = imFriendGroupService.deleteFriendGroup(groupId, principal);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

}
